package cn.dgut.jy.soro.service.impl;

import cn.dgut.jy.soro.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;


@Service
public class MailServiceImpl implements MailService {
    private JavaMailSender mailSender;

    @Autowired
    public MailServiceImpl(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Value("${spring.mail.username}")
    private String from;

    public void sendMail(String to, String subject, String text) throws MessagingException, UnsupportedEncodingException {
        /*SimpleMailMessage message = new SimpleMailMessage();*/
        MimeMessage message1 = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message1, true, "UTF-8");
        messageHelper.setFrom(new InternetAddress(from, "Soro", "UTF-8"));
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(text);
        /*message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);*/
        mailSender.send(message1);
    }
}
